var pager=1,searchVal='',noDatePager=true,sort='popularity';
var carIds = new Array(); // 搜索选中的分类
var brandsIds = new Array(); // 搜索选中的品牌

$(function () {
	searchProduct();
});

$('.searchBar a.mui-btn').on('tap',function(){
	var searchVal=$('#searchVal').val();
	sessionStorage.setItem('searchVal',searchVal);
	carIds = new Array();
	brandsIds = new Array();
	searchProduct();
	mui('#offCanvasContentScroll').scroll().scrollTo(0,0,100);
});

function searchProduct(){
	$('.result p span').html(0);
	$('.list.horizontal-list').html('');

	searchVal=$('#searchVal').val();
	if(searchVal!=''){
		$('.result h4 span').html(searchVal);		
		getDateList(searchVal,pager);
	}else{
		$('.result h4 span').html('');
		sessionStorage.removeItem("searchVal");
		$('.list.horizontal-list').html("<div class='nodate'>'search text' must be entered</div>");
	}
	
//		getInit();
	
	
	$(window).on('swipeup',function(){
		var endTop=$('#footer').offset().top;
		if(endTop<$(window).height()-100){
			getDateList(searchVal,++pager);
		}
	})
}

function　getDateList(val,pager){
	var arr={
		myasync : false,
		hf_s_id:"WD11",
		hf_srv_id:"Pv_jpdt_srch",
		hs_srch_word:val,
		hs_rec_per_page:"5 10",
		hs_sort:sort,
		hvs_cat_id: carIds.join(","),
		hvs_brand_id: brandsIds.join(","),
		hs_srch_page_no:pager+" "+pager,
		hs_action_id:"submit",
		hs_call_ts:"testing"
	};
	getAjaxRequest(arr,function(result){
		// 设置筛选项
		var bvc_vectors = result.bvc_vector;
		$("#fbrand .content").html("");
		$("#fcat .content").html("");
		for (var i = 0; i < bvc_vectors.length; i ++) {
			var bvc_vector = bvc_vectors[i];

			// 搜索结果品牌
			var bvs_jpdt_fbrand_i = bvc_vector.bj_jpdt_fbrand.bvs_jpdt_fbrand_i;
			for (var j = 0; j < bvs_jpdt_fbrand_i.length; j ++) {
				// fbrands.push(bvs_jpdt_fbrand_i[j]);
				var fbrand = bvs_jpdt_fbrand_i[j];
				var active = "";
				if (brandsIds.indexOf(fbrand.bs_brand_id) >= 0) {
					active = " class='active' ";
				}

				$("#fbrand .content").append("<p data-bj_cnt='" +　fbrand.bj_cnt + "' data-bs_brand_id='" + fbrand.bs_brand_id + "' " + active + "><span></span>" + fbrand.bs_brand_name + "</p>");

			}

			// 搜索结果分类
			var bvs_jpdt_fcat_i = bvc_vector.bj_jpdt_fcat.bvs_jpdt_fcat_i;
			for (var j = 0; j < bvs_jpdt_fcat_i.length; j ++) {
				var fcat = bvs_jpdt_fcat_i[j];
				// fcats.push(bvs_jpdt_fcat_i[i]);
				var active = "";
				if (carIds.indexOf(fcat.bs_cat_id) >= 0) {
					active = " class='active' ";
				}

				$("#fcat .content").append("<p data-bj_cnt='" + fcat.bj_cnt + "' data-bs_cat_id='" + fcat.bs_cat_id + "'" + active + "><span></span>" + fcat.bs_cat_name + "</p>");

			}
		}

		// 绑定选中事件
		$('.slideBox p').each(function(){
			$(this).on('tap',function(){
				if($(this).hasClass('active')){
					$(this).removeClass('active');
				}else{
					$(this).addClass('active');
				}
				getSelectNum();
			})
		});

		var productArr=setProductList_Hor(result);
		if(productArr[1]==0){
			if($('.list.horizontal-list .nodate').length<1){
				$('.list.horizontal-list').append("<div class='nodate'>No more data</div>");
			}else{
				$('.list.horizontal-list .nodate').html("No more data");
			}
			$('.result p span').html(0);
		}else{
			$('.list.horizontal-list').append(productArr[0]);
		}
		mui('.mui-numbox').numbox();
		var num=parseInt($('.result p span').html());
		$('.result p span').html(productArr[1]+num);

		//两列数据的列表点击
		$('.horizontal-list .item').each(function(){
			var $this=$(this);
			$this.on('tap','.proImg',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var product_info={};
				product_info.pdt_id=pdt_id;
				product_info.item_no=item_no;
				sessionStorage.setItem('product_info',JSON.stringify(product_info));
				location.href=MPJ.base+"/pages/product/product-detail.jsp";
			})
			$this.on('tap','.addCar',function(){
				var pdt_id=$this.data('id');
				var item_no=$this.data('item-no');
				var arr={
					hf_s_id:"WD11",
					hf_srv_id:"Av_jcart",
					hs_action_id:"add_all_to_cart",
					hvs_add_pdt_id:pdt_id,
					hvs_add_qty:$this.find('.mui-numbox-input').val(),
				};
				getAjaxRequest(arr,function(result){
					if(result.bs_result==BS_RESULT.OK){
						dialog('Message', 'Add shopping cart success!', 1);
						var $dialog = $('#dialog');
						$dialog.show();
						$dialog.find('.comfirmBtn').one('click', function() {
							$dialog.hide();
							$dialog.remove();
							getCartSumCount();
						})
					}

				});
			})
		})
	});
}
$('#sortBy').change(function(){
	sort=$(this).children('option:selected').val();
	searchProduct();
});
$('#filter').on('tap',function(){
	carIds = new Array();
	brandsIds = new Array();

	var _this = this;
	var $slideBox=$('.content .slideBox');
	if($slideBox.css('display')=="none"){
		$(_this).html("Action");
		$slideBox.slideDown();
		$(this).addClass('on');
	}else{
		$(_this).html("Filters");
		$slideBox.slideUp();
		$(this).removeClass('on');

		// $("#fcat .content .active").each(function () {
		// 	var bj_cnt = $(this).data('bj_cnt');
		// 	var bs_cat_id = $(this).data('bs_cat_id');
		// 	carIds.push(bs_cat_id);
		// });

		var $fcat = $("#fcat .content .active");
		for (var i = 0; i < $fcat.length; i ++) {
			var bj_cnt = $($fcat[i]).data('bj_cnt');
			var bs_cat_id = $($fcat[i]).data('bs_cat_id');
			carIds.push(bs_cat_id);
		}

		var $fbrand = $("#fbrand .content .active");
		for (var i = 0; i < $fbrand.length; i ++) {
			var bj_cnt = $($fbrand[i]).data('bj_cnt');
			var bs_brand_id = $($fbrand[i]).data('bs_brand_id');
			brandsIds.push(bs_brand_id);
		}

		// $("#fbrand .content .active").each(function () {
        //
		// });
		console.log(brandsIds);
		console.log(carIds);
		pager = 1;
		$('.list.horizontal-list').html('');
		getDateList(searchVal, pager);
	}
});

$('.list .addCar').on('tap',function(){
	location.href=MPJ.base+"/pages/cart/list.jsp";
})


function getSelectNum(){
	var p=0;
	$('.slideBox p').each(function(){		
		if($(this).hasClass('active')){
			p++;
		}
	})
}

$('.slideBox .item').each(function(){
	var $this=$(this);
	var $span=$this.find('.title span');
	$span.on('tap',function(){
		if($span.hasClass('active')){
			$span.removeClass('active');
			$this.find('.content').slideDown();
		}else{
			$span.addClass('active');
			$this.find('.content').slideUp();
		}
	})
})

//
//function getInit(){
//	mui.init({
//		pullRefresh: {
//			container: '#offCanvasContentScroll',
//			up: {
//				contentrefresh: '正在加载...',
//				callback: pullupRefresh
//			}
//		}
//	});
//}
/////**
//// * 上拉加载具体业务实现
//// */
//function pullupRefresh() {
//	setTimeout(function() {
//		mui('#offCanvasContentScroll').pullRefresh().endPullupToRefresh((noDatePager==false)); //参数为true代表没有更多数据了。
//		if(noDatePager){
//			getDateList(searchVal,pager);
//			pager++;		
//		}
//	}, 1000);
//}
//if (mui.os.plus) {
//	mui.plusReady(function() {
//		setTimeout(function() {
//			mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
//		}, 1000);
//
//	});
//} else {
//	mui.ready(function() {
//		mui('#offCanvasContentScroll').pullRefresh().pullupLoading();
//	});
//}
//
//

